<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@page import="wt.httpgw.URLFactory"%>
<%@page import="wt.session.SessionHelper"%>
<%@page import="wt.org.WTUser"%>
<%@page import="java.text.SimpleDateFormat,java.util.Date,java.util.Calendar;"%>
<%
String baseUrl = new URLFactory().getBaseHREF();
WTUser user = (WTUser)SessionHelper.manager.getPrincipal();
String timeStr = "";
Date dt = new Date();
Calendar cal = Calendar.getInstance();
cal.add(Calendar.HOUR,8);
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
timeStr = f.format(cal.getTime());
%>


<body>
<script>
$(function(){  
	           $('#createReimbursement_no').combogrid({  
	                panelWidth:250,   
	                idField:'number',  
	                textField:'number', 
	                queryParams:{folderName:"/Default/项目管理/项目信息",info:"项目信息",ibas:"ContactNo,ProjectProperties"}, 
	                url:'<%=baseUrl%>shmged/jsp/com/shlcm/documents/jspSearchDocument.jsp',  
                    columns:[[  
	                    {field:'number',title:'Number',width:60},  
	                   {field:'name',title:'Name',width:100},
	                    {field:'ContactNo',title:'ContactNo',width:100,hidden:true}    
	                  
	                ]],
	               onSelect:function(record){
	 	                var r = $(this).datagrid('getSelected');
	 	               // if(r.ContactNo != null){
	 	                	$('#createReimbursement_ContactNo').val(r.ContactNo);  
	 	               // }
                       // alert(r.ContactNo);
		           }   
	           });  
        });  
        

</script>

<center>
<div style="margin:10px 0;"></div>
	<div class="easyui-panel" title="费用报销单" style="width:720px" >
		<div style="padding:10px 0 10px 60px">
<form method="post" enctype="multipart/form-data" name="ReimbursementEntryForm" id=ReimbursementEntryForm>
<table  id="createReimbuserment_mytable"width="650"  >
<input type="hidden" id="createReimbursement_inserted" name="inserted"/>
<input type="hidden" id="createReimbursement_updated" name="updated"/>
<input type="hidden" id="createReimbursement_deleted" name="deleted"/>
<input type="hidden" name="PersonalNo" value=<%=user.getFullName() %> />
<input type="hidden" name="beginTime"  value="<%=timeStr%>" ></input>
  
  <tr>
    <td >项目编号</td>
    <td ><select id="createReimbursement_no" name="ProjectNo" style="width:150px;"></select> 
    
    </td>
    <td >合同编号</td>
    <td ><input type="text" id="createReimbursement_ContactNo" name="ContactNo" size="20" readOnly onkeydown="ieBackSpaceBug()"></input></td>
  </tr>
  <tr>
    <td >时间</td>
    <td ><%=timeStr%></td>
    <td >报销人</td>
    <td ><%=user.getFullName() %></td>
  </tr>
   <tr>
    </tr>
  <tr>
  <td colspan="4">
  <script>
  $("#createReimbuserment_mytable").bind('click',function(){
	  totalSum();
	  });
  function totalSum(){
	  endEdit();
		if ($('#createReimbursement_tt').datagrid('getChanges')) { 
		    var inserted = $('#createReimbursement_tt').datagrid('getChanges', "inserted"); 
		}
		if(inserted !=""){
			var myobj = eval(JSON.stringify(inserted));
		 	var sum = 0;
			if(myobj != ""){ 
				for( var i=0;i<myobj.length; i++){
					if(myobj[i].sum != ""){
					sum = sum + parseFloat(myobj[i].sum);
					}
				}
			}
      		$('#createReimbursement_Sum').val(String(parseFloat(sum).toFixed(2)));
      		if(sum != 0){
      			$('#ChineseSum').val(convertCurrency(sum));
      		}
		}
  }

 $.extend(
	     $.fn.datagrid.defaults.editors, {
	         datebox: {
	             init: function (container, options) {
	                 var input = $('<input type="text">').appendTo(container);
	                 input.datebox(options);
	                 return input;
	             },
	             destroy: function (target) {
	                 $(target).datebox('destroy');
	             },
	             getValue: function (target) {
	                 return $(target).datebox('getValue');
	             },
	             setValue: function (target, value) {
	                 $(target).datebox('setValue', myformatter(value));
	             },
	             resize: function (target, width) {
	                 $(target).datebox('resize', width);
	             },
	             
	            
	         }
	     });

 Date.prototype.format = function (format) 
 {
     var o = {
         "M+": this.getMonth() + 1, //month 
         "d+": this.getDate(),    //day 
         "h+": this.getHours(),   //hour 
         "m+": this.getMinutes(), //minute 
         "s+": this.getSeconds(), //second 
         "q+": Math.floor((this.getMonth() + 3) / 3),  //quarter 
         "S": this.getMilliseconds() //millisecond 
     }
     if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
     (this.getFullYear() + "").substr(4 - RegExp.$1.length));
     for (var k in o) if (new RegExp("(" + k + ")").test(format))
         format = format.replace(RegExp.$1,
       RegExp.$1.length == 1 ? o[k] :
         ("00" + o[k]).substr(("" + o[k]).length));
     return format;
 }
	 


		$(function(){
			var lastIndex;
			var costTypeList = [
			                    {costType:'cw001',name:'差旅费'}, 
			                    {costType:'cw002',name:'住宿费'},
			                    {costType:'cw003',name:'通讯费'},
			                    {costType:'cw004',name:'其他'}   			
			        			]; 
			$('#createReimbursement_tt').datagrid({
				title:'报销明细',
				width:600,
				nowrap: true,
				striped: true,
				showFooter:true,
				//sortName: 'code',
				//sortOrder: 'desc',
				//idField:'code',
				frozenColumns:[[
	                {field:'ck',checkbox:true},
				]],
				columns:[[
					{field:'time',title:'时间',width:100,
						editor:{
							type:'datebox',
							options:{
								formatter:myformatter,
								parser:myparser
						}}
						},
					{field:'costType',title:'费用科目',width:100,
						formatter:function(value){
							for(var i=0;i<costTypeList.length;i++){
								if(costTypeList[i].costType == value){ 
									return costTypeList[i].name;
								}	
						}
							
							return value;
					},editor:{
						type:'combobox',
						options:{
							valueField:'costType',
							textField:'name',
							data:costTypeList,//切忌无单引号
							required:true
					}
					}
					},
					{field:'sum',title:'金额',width:100,editor:{type:'numberbox',options:{precision:2}}},
					{field:'useDescription',title:'用途',width:100,editor:'text'},
					{field:'memo',title:'备注',width:100,editor:'text'}
					
				]],
				
				rownumbers:true,
				singleSelect:true,
				toolbar:[{
					text:'Add',
					iconCls:'icon-add',
					handler:function(){
						$('#createReimbursement_tt').datagrid('endEdit',lastIndex);
						$('#createReimbursement_tt').datagrid('appendRow',{
                            ck:'',
							time:'',
							costType:'',
							sum:'',
							useDescription:'',
							memo:''
					});
						lastIndex = $('#createReimbursement_tt').datagrid('getRows').length-1;  
						$('#createReimbursement_tt').datagrid('selectRow', lastIndex);  
						$('#createReimbursement_tt').datagrid('beginEdit', lastIndex); 
						totalSum(); 
				}	
												
				},'-',{
					text:'Cut',
					iconCls:'icon-cut',
					//disabled:true,
					handler:function(){
						var row = $('#createReimbursement_tt').datagrid('getSelected');
						if (row) {
							var index = $('#createReimbursement_tt').datagrid('getRowIndex',row);
							$('#createReimbursement_tt').datagrid('deleteRow',index);
							totalSum();
					}
				  }
				}],
				onBeforeLoad:function(){
					$(this).datagrid('rejectChanges');
				},
				onClickRow:function(rowIndex){
					if(lastIndex != rowIndex){
						$('#createReimbursement_tt').datagrid('endEdit',lastIndex);
						$('#createReimbursement_tt').datagrid('beginEdit',rowIndex);
						
						}
					lastIndex = rowIndex;
					}
				
				
			});
			
		});
		
		function getSelected(){
			var selected = $('#createReimbursement_tt').datagrid('getSelected');
			alert(selected.code+":"+selected.name);
		}
		function getSelections(){
			var ids = [];
			var rows = $('#createReimbursement_tt').datagrid('getSelections');
			for(var i=0;i<rows.length;i++){
				ids.push(rows[i].code);
			}
			alert(ids.join(':'))
		}
		function clearSelections(){
			$('#createReimbursement_tt').datagrid('clearSelections');
		}
		function selectRow(){
			$('#createReimbursement_tt').datagrid('selectRow',2);
		}
		function selectRecord(){
			$('#createReimbursement_tt').datagrid('selectRecord','002');
		}
		function unselectRow(){
			$('#createReimbursement_tt').datagrid('unselectRow',2);
		}

		 

		
	</script>
  
 
  <table id="createReimbursement_tt"  title="报销明细">
  </table>
  </td>
  </tr>
 
  <tr>
    </tr>
  <tr>	
  	<td >报销金额(大写)</td>
    <td ><input type="text"  id="ChineseSum" size="20" readOnly onkeydown="ieBackSpaceBug()"></input></td>
    <td >报销金额(小写)</td>
    <td ><input type="text" id="createReimbursement_Sum" name="Sum" size="20"  readOnly onkeydown="ieBackSpaceBug()"></input></td>
  </tr>
  <tr>
  	<td >付款方式</td>
    <td ><select id="createReimbusrsement_cc" name="PayMethod" required="true">
	<option value="现金支付">现金支付</option>
	<option value="银行转账">银行转账</option>
</select>
    </td>
    <td >附件数</td>
    <td ><input type="text" id="AttachmentNum" name="AttachmentNum" size="20"></input></td>
  </tr>
  <tr>
  	<td >备注</td>
    <td colspan="3"><textarea name="Memo" id="Memo" cols="57" rows="5"></textarea></td>
  </tr>
 <tr>
      <td align="center" colspan="4"><label>
        <a href="javascript:void(0)" class="easyui-linkbutton" onclick="submitReimbursementEntryForm()">提交</a>
	    	<a href="javascript:void(0)" class="easyui-linkbutton" onclick="clearForm1()">清除</a>
      </label></td>
    </tr>
</table>
</form>
</div>
</div>
</center>
<script type="text/javascript">

    
    function endEdit(){
		var rows = $('#createReimbursement_tt').datagrid('getRows');
		for (var i=0;i<rows.length;i++){
			$('#createReimbursement_tt').datagrid('endEdit',i);
		}
    }

    function saveDataGrid(){
		endEdit();

		if ($('#createReimbursement_tt').datagrid('getChanges').length) { 
		    var inserted = $('#createReimbursement_tt').datagrid('getChanges', "inserted"); 
            var deleted = $('#createReimbursement_tt').datagrid('getChanges', "deleted"); 
		    var updated = $('#createReimbursement_tt').datagrid('getChanges', "updated"); 
		    var effectRow = new Object(); 
		    alert(JSON.stringify(inserted));
		   if (inserted.length) { 
		       effectRow["inserted"] = JSON.stringify(inserted); 
		    } 
		    if (deleted.length) { 
		       effectRow["deleted"] = JSON.stringify(deleted); 
		      } 
           if (updated.length) { 
		      effectRow["updated"] = JSON.stringify(updated); 
		} 
		 $.post("<%=baseUrl%>shmged/jsp/com/shlcm/part/jspSaveDataGrid.jsp", effectRow, function(response) { 
		 if(response.status){ 
		 $.messager.alert("提示", "提交成功！"); 
		 $('#createReimbursement_tt').datagrid('acceptChanges'); 
		 } 
		}, "JSON").error(function() { 
		  $.messager.alert("提示", "提交错误了！"); 
		 }); 
		  
    }
    }

    function prepareDataGridData(){
    	endEdit();

		if ($('#createReimbursement_tt').datagrid('getChanges').length) { 
		    var inserted = $('#createReimbursement_tt').datagrid('getChanges', "inserted"); 
            var deleted = $('#createReimbursement_tt').datagrid('getChanges', "deleted"); 
		    var updated = $('#createReimbursement_tt').datagrid('getChanges', "updated");
		    $('#createReimbursement_inserted').val(JSON.stringify(inserted));
		    $('#createReimbursement_deleted').val(JSON.stringify(deleted)); 
		    $('#createReimbursement_updated').val(JSON.stringify(updated));  
		}
    }

	function submitReimbursementEntryForm()
	{
		//
		//alert($('#createReimbursement_Sum').val());
		
		initReimbursementEntryForm();
		//$('#ReimbursementEntryForm').submit();
		//saveDataGrid();
	}
	

	function initReimbursementEntryForm(){
		$("#ReimbursementEntryForm").form('submit',{
			url:'<%=baseUrl%>shmged/jsp/com/shlcm/part/jspCreateReimbursementEntry.jsp',
			onSubmit:function (){
				prepareDataGridData();
				totalSum();
			},
			success:function (data){
				//alert(data);
				if(data.indexOf("ERROR") != -1){
					$.messager.alert('提示',"创建失败");
				}else{
					var oid=data.split("__");
					$.messager.alert('提示',oid[0]);
					$('#cmTabs').tabs('close','费用报销');
					refeHome();
					//addTab1("","",oid[1],oid[2]);
				}
				//hideProcessMsg("prince_process_ques");
			},
			onLoadError:function (){
				//hideProcessMsg("prince_process_ques");
			}
		});
	}

	//

	function refeHome(){
		$("#cmTabs").tabs('select',"首页信息");  
        var tab = $('#cmTabs').tabs('getSelected');       
        $("#cmTabs").tabs('update',{  
        tab:tab,  
        options:{  
           title:'首页信息',  
           style:{padding:'1px'},  
       //href:URL, // 使用href会导致页面加载两次，所以使用content代替   
           content:'<iframe name="indextab" scrolling="auto" frameborder="0" style="width:100%;height:100%;"></iframe>',  
       closable:false,  
       fit:true,  
       selected:true,  
       closable:true
        }  
         });  
	}
		
</script>
</body>

</html>


